<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.nickname" clearable class="filter-item" style="width: 200px;"
        :placeholder="$t('Name')" />
      <el-select v-model="listQuery.shopId" clearable class="filter-item" :placeholder="$t('请选择门店')">
        <el-option v-for="item in shops" :value="item.id" :label="item.name"></el-option>
      </el-select>
      <el-select v-model="listQuery.roleId" clearable class="filter-item" :placeholder="$t('请选择角色')">
        <el-option v-for="item in roleOptions" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-button class="filter-item" type="info" plain1 icon="el-icon-search" @click="handleFilter">{{ $t('Search') }}
      </el-button>
      <el-button v-permission="['GET /admin/p/store_user_add', 'GET /admin/p/system_controllerlist_add']"
        class="filter-item" type="warning" plain1 icon="el-icon-circle-plus" @click="handleCreate">{{ $t('Add') }}</el-button>
      <!--      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">{{$t('Find')}}</el-button>-->
    </div>


    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" :element-loading-text="$t('Searching')" stripe fit border
              :header-cell-style="{background:'#eef1f6',color:'#606266',top:'5px','font-size':'15px'}"
      highlight-current-row>
      <el-table-column type="index" fixed="left" :index="getIndex" width="50"></el-table-column>
      <el-table-column align="center" width="150px" :label="$t('Store_belong')">
        <template slot-scope="scope">
          <span v-for="item in shops" v-if="item.id == scope.row.shopId">
            {{ item.name }}
          </span>
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="left" width="150px" :label="$t('Partner_name')" prop="nickName" />
      <el-table-column width="120px" align="center"  property="avatar" :label="$t('头像')">
        <template slot-scope="scope">
          <el-image preview-src-list style="border-radius:15%;width:40px" :src="scope.row.avatar"  width="20" lazy>
            <div slot="error" class="image-slot">
              <i style="font-size: 30px" class="el-icon-picture-outline-round"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('Account_login')" prop="username">
        <template slot-scope="scope">
          <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.username:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('员工代号')" prop="code">
        <template slot-scope="scope">
          <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.code:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('社保号码')" prop="socialSecurityNumber">
        <template slot-scope="scope">
          <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.socialSecurityNumber:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="100px" :label="$t('性别')" prop="gender">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender == 1" type="waring" style="margin-right: 20px;"> {{ $t('其他') }} </el-tag>
          <el-tag v-else-if="scope.row.gender == 2" type="danger" style="margin-right: 20px;"> <i class="el-icon-female"></i></el-tag>
          <el-tag v-else-if="scope.row.gender == 3" type="primary" style="margin-right: 20px;"> <i class="el-icon-male"></i> </el-tag>
          <el-tag v-else type="info" style="margin-right: 20px;"> {{ $t('保密') }} </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('生日')" prop="birthday">
        <template slot-scope="scope">
           <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.birthday:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('Address')" prop="location">
        <template slot-scope="scope">
           <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.location:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('Partner_role')" prop="roleIds">
        <template slot-scope="scope">
          <el-tag v-for="roleId in scope.row.roleIds" type="primary" style="margin-right: 20px;"> {{ formatRole(roleId)
          }} </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" width="200px" :label="$t('Time_added')" prop="addTime">
        <template slot-scope="scope">
          <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.addTime:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('Contact_number')" prop="mobile">
        <template slot-scope="scope">
          <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.mobile:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" width="150px" :label="$t('邮箱')" prop="email">
        <template slot-scope="scope">
          <el-link :underline="false" @focus="test($event)">{{scope.row.readInfo?scope.row.email:'******'}}</el-link>
        </template>
      </el-table-column>

      <el-table-column fixed="right" align="center" :label="$t('Operate')" width="250"  class-name="small-padding fixed-width">
        <template slot="header" slot-scope="{row}">
          <i class="el-icon-setting"></i>
          {{$t('Operate')}}
        </template>
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" plain1 v-permission="['GET /admin/p/store_user_edit', 'GET /admin/p/system_controllerlist_edit']" size="mini" @click="handleUpdate(scope.row)">{{ $t('Edit') }}</el-button>
          <el-button icon="el-icon-delete" plain1 v-permission="['GET /admin/p/store_user_delete', 'GET /admin/p/system_controllerlist_delete']"
            type="danger" size="mini" @click="handleDelete(scope.row)">{{ $t('Delete') }}</el-button>
          <el-button icon="el-icon-view" plain1 v-permission="['GET /admin/p/store_user_delete', 'GET /admin/p/system_controllerlist_delete']"
                     :type="scope.row.readInfo?'info':'primary'" size="mini" @click="scope.row.readInfo=!scope.row.readInfo">{{ scope.row.readInfo?$t('隐藏'):$t('查看') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px"
        style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('管理员名称')" prop="username">
          <el-input v-model="dataForm.username" />
        </el-form-item>
        <el-form-item :label="$t('管理员密码')" prop="password">
          <el-input v-model="dataForm.password" type="password" auto-complete="off" />
        </el-form-item>
        <el-form-item :label="$t('管理员头像')" prop="avatar">
          <el-upload :headers="headers" :action="uploadPath" :show-file-list="false" :on-success="uploadAvatar"
            class="avatar-uploader" accept=".jpg,.jpeg,.png,.gif">
            <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item :label="$t('管理员角色')" prop="roleIds">
          <el-select v-model="dataForm.roleIds" multiple :placeholder="$t('Please_select')">
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('Cancel') }}</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData">{{ $t('Confirm') }}</el-button>
        <el-button v-else type="primary" @click="updateData">{{ $t('Confirm') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<style scoped>
.el-input {
  border: none;
}

.el-input__inner {
  border: none;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import { listAdminv2, createAdmin, updateAdmin, deleteAdmin } from '@/api/admin'
import { roleOptions } from '@/api/role'
import { uploadPath } from '@/api/storage'
import { getToken } from '@/utils/auth'
import { allForPerm } from '@/api/shop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination


export default {
  name: 'sysAdmin',
  components: { Pagination },
  data() {
    return {
      uploadPath,
      list: [],
      total: 0,
      roleOptions: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        nickname: undefined,
        shopId: undefined,
        roleId: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        username: undefined,
        password: undefined,
        avatar: undefined,
        roleIds: []
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: this.$t('Edit'),
        create: this.$t('Create')
      },
      rules: {
        username: [
          { required: true, message: this.$t('Controller_name_cannot_be_empty'), trigger: 'blur' }
        ],
        password: [{ required: true, message: this.$t('Password_cannot_be_empty'), trigger: 'blur' }]
      },
      downloadLoading: false,
      shops: [],
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.getList()

    roleOptions()
      .then(response => {
        this.roleOptions = response.data.data.list
      })
    allForPerm().then(response => {
      this.shops = response.data.data.list
    })
  },
  methods: {
    test(e) {
      e.target.blur();
    },
    formatRole(roleId) {
      for (let i = 0; i < this.roleOptions.length; i++) {
        if (roleId === this.roleOptions[i].value) {
          return this.roleOptions[i].label
        }
      }
      return ''
    },
    getList() {
      this.listLoading = true
      if (this.$route.query.shopId) {
        let shopId = this.$route.query.shopId;
        this.listQuery.shopId = shopId;
      }
      listAdminv2(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        username: undefined,
        password: undefined,
        avatar: undefined,
        roleIds: []
      }
    },
    uploadAvatar: function (response) {
      this.dataForm.avatar = response.data.url
    },
    handleCreate() {
      /*      this.resetForm()
            this.dialogStatus = 'create'
            this.dialogFormVisible = true
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })*/

      this.$router.push({ path: '/shop/admin/create', query: { shopId: this.listQuery.shopId } })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createAdmin(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: this.$t('Success!'),
                message: this.$t('Controller_added_successfully')
              })
            })
            .catch(response => {
              this.$notify.error({
                title: this.$t('Failed'),
                message: this.$t(response.data.errmsg)
              })
            })
        }
      })
    },
    handleUpdate(row) {
      /*      this.dataForm = Object.assign({}, row)
            this.dialogStatus = 'update'
            this.dialogFormVisible = true
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })*/
      this.$router.push({ path: '/shop/admin/edit', query: { id: row.id, shopId: this.listQuery.shopId } })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updateAdmin(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: this.$t('Success!'),
                message: this.$t('更新管理员成功')
              })
            })
            .catch(response => {
              this.$notify.error({
                title: this.$t('Failed'),
                message: this.$t(response.data.errmsg)
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('Confirm_Delete?'), this.$t('Hint'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: 'warning'
      }).then(() => {
        this.doDelete(row)
      });
    },
    doDelete(row) {
      deleteAdmin(row)
        .then(response => {
          this.$notify.success({
            title: this.$t('Success!'),
            message: this.$t('Controller_deleted')
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
        .catch(response => {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t(response.data.errmsg)
          })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['管理员ID', '管理员名称', '管理员头像']
        const filterVal = ['id', 'username', 'avatar']
        excel.export_json_to_excel2(
          tHeader,
          this.list,
          filterVal,
          '管理员信息'
        )
        this.downloadLoading = false
      })
    },
    getIndex(index) {
      return (index + 1) + (this.listQuery.page - 1) * this.listQuery.limit;
    }
  }
}
</script>
